<?php
namespace Realisation\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Description of MessageShow
 *
 * @author pahhan
 */
class UrlInfo extends Command
{
    protected function configure()
    {
        $this
            ->setName('url:info')
            ->setDescription('Show url information for given id')
            ->addArgument('id', InputArgument::REQUIRED, 'Url id')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $app = \Realisation\App::getInstance();

        /* @var $conn \Doctrine\DBAL\Connection */
        $conn = $app['db'];

        $url = $conn->fetchAssoc('
            SELECT u.*, s.title site_title
            FROM url u
            LEFT JOIN site s ON s.id = u.site_id
            WHERE u.id = ?
            ', array($input->getArgument('id')));

        if( empty($url) ) {
            $output->writeln('<error>Url not found</error>');
            return;
        }

        $url['message_count'] = $conn->fetchColumn('SELECT COUNT(*) FROM message WHERE url_id = ?', array($input->getArgument('id')));


        $output->writeln(print_r($url, TRUE));

    }
}

